package com.pj.app.aura;


import com.pj.lib.aura.connection.Result;
import com.pj.lib.aura.protocol.AuraProtocolMapKeys;
import com.pj.lib.auraClient.Aura;
import com.pj.lib.auraClient.AuraConfiguration;
import com.pj.lib.auraClient.AuraPreferences;
import com.pj.lib.auraClient.API.AuraCaster;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	Button button;
	private AuraCaster ac;
	private EditText editTextLogin;
	private EditText editTextPassword;
	private Button buttonLogin;
	private Button buttonCreateAccount;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        
        setWidgets();
        
        setPreferences();
        
       /* ac = Aura.getInstance());
        
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Result result = ac.login("user1", "password");
				Toast.makeText(LoginActivity.this, "result: " + result.getResult() + "; description: " + result.getDescription()+ "; userId: " + result.getValue(AuraProtocolMapKeys.USER_ID_KEY), Toast.LENGTH_LONG).show();
			}
		});*/
    }


	private void setWidgets() {
		editTextLogin = (EditText) findViewById(R.id.login_username_field);
		editTextPassword = (EditText) findViewById(R.id.login_password_field);
		buttonLogin = (Button) findViewById(R.id.login_button);
		buttonCreateAccount = (Button) findViewById(R.id.login_create_account);
		
		buttonCreateAccount.setOnClickListener(new OnCreateAccount());
		buttonLogin.setOnClickListener(new OnLogin());
	}
	

	private void setPreferences() {
		SharedPreferences settings = getSharedPreferences(AuraPreferences.NAME, Context.MODE_PRIVATE);
	    String login = settings.getString(AuraPreferences.KEY_LOGIN, "");
	    String pass = settings.getString(AuraPreferences.KEY_PASSWORD, "");
	    editTextLogin.setText(login);
	    editTextPassword.setText(pass);
	}	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == Activity.RESULT_OK) {
			Toast.makeText(this, R.string.create_account_successful, Toast.LENGTH_LONG).show();
			editTextLogin.setText(data.getStringExtra(CreateAccountActivity.NICK_NAME));
			editTextPassword.setText(data.getStringExtra(CreateAccountActivity.PASSWORD));
		}
	}



	private class OnCreateAccount implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent in = new Intent(v.getContext(), CreateAccountActivity.class);
			startActivityForResult(in, 0);
		}
		
	}

	private class OnLogin implements OnClickListener {

		@Override
		public void onClick(View v) {
			Boolean login = true;
			if(editTextLogin.getText().toString().equals("")) {
				editTextLogin.setError(getText(R.string.field_cannot_be_null));
				login = false;
			}
			if(editTextPassword.getText().toString().equals("")) {
				editTextPassword.setError(getText(R.string.field_cannot_be_null));
				login = false;
			}
			if(login) {
				SharedPreferences settings = getSharedPreferences(AuraPreferences.NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString(AuraPreferences.KEY_LOGIN, editTextLogin.getText().toString());
				editor.putString(AuraPreferences.KEY_PASSWORD, editTextPassword.getText().toString());
				
				editor.commit();
				
				new LoginTask().execute(new String[] {editTextLogin.getText().toString(), editTextPassword.getText().toString()});
			}

		}
		
	}
	
	private class LoginTask extends AsyncTask<String, Void, Result> {
		ProgressDialog dialog;
		
		@Override
		protected void onPreExecute() {
			dialog = ProgressDialog.show(LoginActivity.this, getText(R.string.app_name), getText(R.string.aura_login), false, true, new OnCancelProgressDialog());
		}

		@Override
		protected Result doInBackground(String... params) {
			Result result = Aura.getInstance().getUserAPI().login(params[0], params[1]);
			return result;
		}

		@Override
		protected void onPostExecute(Result result) {
			dialog.dismiss();
			if(result.getResult() == true) {
				goToNextActivity();
			} else {
				if(result.getDescription().equals("lib_login_failed")) {
					Toast.makeText(LoginActivity.this, getText(R.string.login_failed), Toast.LENGTH_LONG).show();
				} else if(result.getDescription().equals("lib_server_error")) {
					Toast.makeText(LoginActivity.this, getText(R.string.server_error), Toast.LENGTH_LONG).show();
				}
			}
		}
		
		private class OnCancelProgressDialog implements OnCancelListener {

			@Override
			public void onCancel(DialogInterface dialog) {
				LoginTask.this.cancel(true);
			}
			
		}
	}

	public void goToNextActivity() {
		//TODO to next activity
		
	}
}